<template>

	<view class="content">
		<view class="search-panal">
			<view class="lft-panal">
				<view class="text-area">
					<text class="title" @click="selectAddress">{{address || '请选择你的地址'}}</text>
				</view>
				<itmister-address-picker ref="addressEl" @confirmChange="confirmChange"></itmister-address-picker>
			</view>
			<view class="rgt-panal">
				<view class="search-box">
					<view class="search-input">
						<image src="/static/images/common/search-icon.png" class="search-icon"></image>
						<input type="text" v-model="keyword" placeholder="搜小镜界" placeholder-class="placeholder"
							@input="handleKeywordInput">
						<image v-if="keyword" src="/static/images/common/image-delete.png" class="close-icon"
							@tap="clear" />
					</view>
					<view class="ml-30" @tap="hide">取消</view>
				</view>
			</view>
		</view>
		<view class="map-panal" v-if="shopMapPanal">
			<hchPosition ref="map" :storeData="storeData" :markers="markers"></hchPosition>
		</view>
		<div :class="{'shousuo-line':shopMapPanal==true,'shousuo-line2':shopMapPanal==false}" @click="shousuoMap()"></div>
		<view :class="{'shopInfo-panal':1==1,'shopInfo-panal-h1':shopMapPanal==true,'shopInfo-panal-h2':shopMapPanal==false} ">
			<scroll-view scroll-y style="height: 100%;">
				<view :class="{'cu-item':1==1,'cu-item-on':currShopId==item.id,'cu-item-zanting':item.auditstatus==0}"
					v-for="(item,index) in storeData" :key="index" @click="shopOnMap(item)">
					<view class="flex p-xs margin-bottom-sm mb-sm align-center">
						<view class="lft-panal" style="border-right: 2upx solid #eeeeee;">
							<view>
								<view class="title">{{item.name}}
									<view style="float: right;" @click="viewShop(item)" title='导航前往'>
										<image src="/static/images/order/icon_map.png" class="map-icon"></image>
									</view>

								</view>
							</view>
							<view class="padding-top-xs">距离您4.3km</view>
							<view class="padding-top-xs text-gray line">{{item.address}}</view>
							<view class="padding-top-xs text-gray">营业时间：{{item.bessuniesTime}}</view>
							<view class="padding-top-xs text-gray">服务电话：{{item.tel}}</view>
						</view>
						<view class="rgt-panal">
							<button type="primary" style="margin-top: 80rpx;" class="font-size-lg" hover-class="none"
								@click="toShop(item)">进店</button>
							<!-- <view class="text-bold text-center" @click="toShop(item)">去下单</view> -->
							<!-- 						<navigator url="../../index" open-type="switchTab">
							<view class="text-bold text-center" >去下单</view>
						</navigator> -->
						</view>
					</view>
				</view>
			</scroll-view>
		</view>

	</view>
</template>

<script>
	import hchPosition from "../../../../components/hch-position/hch-position.vue"
	import itmisterAddressPicker from '@/components/itmister-address-picker/itmister-address-picker.nvue'
	import {
		mapState,
		mapMutations
	} from 'vuex'

	//微信小程序中 app.json
	// "permission":{
	//     "scope.userLocation":{
	//       "desc":"请点击确定"
	//     }
	//   },
	export default {
		data() {
			return {
				shopMapPanal: true,
				currShopId: 0, //当前选中店铺id
				keyword: '', //店铺名称关键字查询
				areaId: '', //区域id查询
				address: '',
				shops: [],
				storeData: [],
				markers: [],
			}
		},
		components: {
			hchPosition,
			itmisterAddressPicker
		},
		async onLoad() {
			//获取店铺数据
			this.getShops('areaId=&shopName=&coid=');
			this.currShopId = this.shop.id;
		},
		computed: {
			...mapState(['shop'])
		},
		methods: {
			...mapMutations(['SET_SHOP', 'SET_CHANGESHOP']),
			shousuoMap() {
				this.shopMapPanal = !this.shopMapPanal;
			},
			//在地图中显示商店
			shopOnMap(item) {
				this.currShopId = item.id;
				this.$refs.map.latitude = item.latitude;
				this.$refs.map.longitude = item.longitude;

				this.$refs.map.store = item;
			},
			//进入商店
			toShop(e) {
				if (e.auditstatus == 0) {
					uni.showModal({
						content: e.auditreason + ",请还一家店铺看看吧~",
						confirmColor: '#DBA871',
						success: res => {
							if (res.confirm) {

							}
						}
					})
				} else {
					this.SET_SHOP(e);
					this.SET_CHANGESHOP(true);
					let url = `/pages/index/index?scene=${e.id}`;
					console.log(url);
					uni.switchTab({
						url: url
					});
				}
			},
			//地图导航
			viewShop(item) {
				let plugin = requirePlugin('routePlan');
				let key = 'B4SBZ-Q6HKQ-2VF5Z-GE2PK-ZNMV3-F7FF2'; //使用在腾讯位置服务申请的key
				let referer = '小镜界'; //调用插件的app的名称
				let endPoint = JSON.stringify({ //终点
					'name': item.name,
					'latitude': item.latitude,
					'longitude': item.longitude
				});
				wx.navigateTo({
					url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
				});
			},
			//关键字查询
			handleKeywordInput() {
				//获取店铺数据
				this.getShops(`areaId=${this.areaId}&shopName=${this.keyword}&coid=`);
			},
			//获取店铺数据
			getShops(whe) {
				let that = this;
				let cateOp = {
					url: `api/Lx_Self_CoInfo/UI_GetPageData?${whe}`,
					data: {}
				}

				this.$request.Post(cateOp, (ret) => {
					if (ret.data.length > 0) {
						let rows = ret.data;
						//that.shops = rows;
						that.storeData = [];
						that.markers = [];

						rows.forEach(e => {
							let currStoreData = {
								id: e.coId,
								name: e.coName,
								address: e.coAddr,
								tel: e.coPhone,
								bessuniesTime: e.coBusinessHours,
								latitude: e.coLatitude,
								longitude: e.coLongitude,
								auditstatus: e.auditstatus,
								auditreason: e.auditreason
							}
							that.storeData.push(currStoreData);

							let currMarkers = {
								id: e.coId,
								latitude: e.coLatitude,
								longitude: e.coLongitude,
								iconPath: '../../static/hch-position/md.png',
								callout: {
									content: e.coName,
									borderRadius: 10,
									padding: 10,
									display: "ALWAYS",
								}
							}
							that.markers.push(currMarkers);
						});


					}
				});
			},
			// 打开窗口
			selectAddress() {
				this.$refs.addressEl.show();
			},
			// 确认选中
			confirmChange(address) {
				//debugger;
				this.areaId = address.areaIndex;
				this.address = address.province + '/' + address.city + '/' + address.area;
				//获取店铺数据
				this.getShops(`areaId=${address.areaIndex}&shopName=&coid=`);
			}
		},
		onShow() {

		}
	}
</script>

<style lang="scss">
	.search-panal {
		width: 100%;
		height: 80upx;
		line-height: 80upx;

		.lft-panal {
			width: 50%;
			height: 80upx;
			line-height: 80upx;
			padding: 0upx 20upx;
			float: left;
		}

		.rgt-panal {
			width: 50%;
			height: 80upx;
			line-height: 80upx;
			text-align: right;
			float: right;

			.search-box {
				height: 80rpx;
				padding: 0upx 20upx;
				display: flex;
				align-items: center;
				//border-bottom: 1rpx solid rgba($color: $border-color, $alpha: 0.6);

				.search-input {
					flex: 1;
					height: 60rpx;
					padding: 10rpx 20rpx;
					background-color: #f1f1f1;
					font-size: $font-size-base;
					border-radius: 50rem !important;
					display: flex;
					align-items: center;

					.search-icon {
						width: 30rpx;
						height: 30rpx;
						margin-right: 10rpx;
					}

					input {
						flex: 1;
						text-align: left;
					}

					.close-icon {
						width: 30rpx;
						height: 30rpx;
						margin-left: 20rpx;
					}
				}
			}
		}
	}

	.line {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.map-panal {}

	.shousuo-line {
		width: 100%;
		height: 16px;
		// border: 1px solid #C8C7CC;
		background: url('/static/images/common/shangla.png');
		background-repeat: no-repeat;
		background-position: center center;
		background-color: #fff;
	}
	
	.shousuo-line2 {
		width: 100%;
		height: 16px;
		// border: 1px solid #C8C7CC;
		background: url('/static/images/common/xiala.png');
		background-repeat: no-repeat;
		background-position: center center;
		background-color: #fff;
	}

	.shopInfo-panal-h1 {
		height: calc(100% - 370px);
	}

	.shopInfo-panal-h2 {
		height: calc(100% - 60px);
	}

	.shopInfo-panal {
		width: 100%;
		//min-height: 600upx;
		border: 1px solid #f1f1f1;
		position: fixed;
		bottom: 0;
		//height: calc(100% - 410px);

		background-color: #f1f1f1;
		font-size: 15px;
		color: #333333;
		font-family: Helvetica Neue, Helvetica, sans-serif;

		.cu-item {
			display: block;
			background-color: #ffffff;
			overflow: hidden;
			border-radius: 5px;
			margin: 16px;

			.lft-panal {
				padding: 11px;
				width: 70%;
				border-radius: 3px;
				box-sizing: border-box;
				height: 260upx;
				float: left;

				.title {
					font-weight: bold;
					text-overflow: ellipsis;
					white-space: nowrap;
					overflow: hidden;

					.map-icon {
						width: 42rpx;
						height: 42rpx;
					}

				}

				.padding-top-xs {
					padding-top: 5px;
				}

				.text-gray {
					color: #aaaaaa;
				}
			}

			.rgt-panal {
				padding: 11px;
				margin: 5px;
				width: 25%;
				border-radius: 3px;
				height: 260upx;
				line-height: 260upx;
				float: right;

				.cu-tag {}
			}
		}

		.cu-item-on {
			border: 1px solid #DBA871;
			background: url('/static/images/common/gou.png');
			background-repeat: no-repeat;
			background-position: bottom right;
		}

		.cu-item-zanting {
			border: 1px solid #C8C7CC;
			background: url('/static/images/common/wait.png');
			background-repeat: no-repeat;
			background-position: top right;
		}
	}
</style>
